<template>
    <div id="mybody">
        <div class="login-box">
            <h2>欢迎登录</h2>
            <form>
                <div class="login-field">
                    <input type="text" v-model="loginForm.username" />
                    <label>用户名</label>
                </div>
                <div class="login-field">
                    <input type="password" v-model="loginForm.password" />
                    <label>密码</label>
                </div>
                <button type="button" @click="login">登&emsp;&emsp;录</button>
            </form>
        </div>
    </div>

</template>

<script>
export default {
    data() {
        return {
            loginForm: {
                username: "lanqiao_web_tg",
                password: "songzt@lanqiao.org"
            }
        }
    },
    methods: {
        // axios 的post方式 就是application/json
        login() {
            this.axios.post('http://47.241.102.141:3000/login', {
                username: this.loginForm.username,
                password: this.loginForm.password
            })
                .then(response => {
                    alert(response.data.message);
                    console.log(response.data);
                    if(response.data.success) {
                        //1.登录成功后，拿到一个凭证并存起来
                        // 取的时候 用  JSON.parse(data.data);解析
                        // window.localStorage.setItem("token_key",JSON.stringify(data.data));//整个对象序列化
                        // window.sessionStorage.setItem("token_key",JSON.stringify(response.data.data));
                        this.$store.dispatch("asclogin",{data:JSON.stringify(response.data.data)});
                        //sessionStorage  会话存储，关闭页面后即清除 
                        //2.存储完token之后，才跳转页面
                        this.$router.replace({
                            path:'/dashboard/homePage'
                        });
                    } 
                })
                .then(function (error) {
                    console.log(error);
                });
        },
    }
}
</script>

<style>
@import url(../assets/css/user-login.css);
</style>